package com.example.basic;

public class Dead implements Runnable{
    Phone phone = new Phone();
    Computer computer = new Computer();
    @Override
    public void run() {
        if(Thread.currentThread().getName().equals("A")){
            Thread.yield();
            synchronized (phone){
                System.out.println(Thread.currentThread().getName()+"拿到了phone我现在需要computer");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (computer){
                    System.out.println("我拿到了computer");
                }
            }
        }else {
            synchronized (computer){
                System.out.println(Thread.currentThread().getName());
                synchronized (computer){
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("我也全部拿到了");
                }
            }
        }
    }

    public static void main(String[] args) {
        Dead dead = new Dead();
        new Thread(dead,"A").start();
        new Thread(dead,"B").start();
    }
}
class Phone{


}
class Computer{

}
